﻿
using System;
using System.Collections.Generic;

namespace Test30.Engine
{
    public class ItemType : ContentBase, IComparable<ItemType>
    {
        public override string Name => _name.Singular;
        public Roller Quantity { get; }
        public object Appearance { get; }
        public Roller Charges { get; }
        public Attack Attack { get; }
        public int Armor { get; }
        public ChargeType ChargeType { get; }
        public ItemScript Use { get; }
        public int Price { get; }
        public FlagCollection Flags => new FlagCollection();
        public int LightRadius => _lightRadius;
        public string Noun => Name;
        public ItemTarget Target { get; private set; }
        public string Ammunition { get; private set; }
        public string Category
        {
            get
            {
                if (!String.IsNullOrEmpty(_subcategory)) return _subcategory;

                return _category;
            }
        }
        public ItemType(Content content, string name, string category, string subcategory,
            object appearance, Roller quantity, Roller charges, Attack attack, int armor,
            ChargeType chargeType, ItemScript use, int price)
            : base(content)
        {
            _name = new Noun(name);
            _category = category;
            _subcategory = subcategory;
            Appearance = appearance;
            Quantity = quantity;
            Charges = charges;
            Attack = attack;
            Armor = armor;
            ChargeType = chargeType;
            Use = use;
            Price = price;
            Target = ItemTarget.None;
        }
        public ItemScript GetHitScript(Element element)
        {
            if (_hitScripts.ContainsKey(element)) return _hitScripts[element];

            // no script
            return null;
        }
        public void SetLightRadius(int light) { _lightRadius = light; }
        public void SetTarget(ItemTarget target) { Target = target; }
        public void SetAmmunition(string ammunition) { Ammunition = ammunition; }
        public void SetHitScript(Element element, ItemScript script)
        {
            if (script != null)
            {
                _hitScripts[element] = script;
            }
        }
        #region IComparable<ItemType> Members
        public int CompareTo(ItemType other)
        {
            // compare the categories first
            int category = String.Compare(_category, other._category, StringComparison.Ordinal);
            if (category != 0) return category;

            // then the prices
            if (Price != other.Price) return Price.CompareTo(other.Price);

            // then the names
            return String.Compare(Name, other.Name, StringComparison.Ordinal);
        }
        #endregion

        private Noun _name;
        private int _lightRadius = -1;
        private readonly string _category;
        private readonly string _subcategory;
        private readonly Dictionary<Element, ItemScript> _hitScripts = new Dictionary<Element, ItemScript>();
    }
}
